package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.BaiduImageRecognitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/api/translate")
public class ImageTranslateController extends BaseController {

    @Autowired
    private BaiduImageRecognitionService baiduImageRecognitionService;

    @PostMapping("/image")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            // 创建临时文件（JVM 退出后会自动删除）
            File tempFile = File.createTempFile("upload_", "_" + file.getOriginalFilename());
            file.transferTo(tempFile); // 将文件写入临时文件

            // 调用百度图像识别 API
            String translatedText = baiduImageRecognitionService.recognizeObjectFromImage(tempFile);

            // 删除临时文件
            tempFile.deleteOnExit();

            return AjaxResult.success("translatedText", translatedText);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("图片处理失败: " + e.getMessage());
        }
    }

}
